package edu.faytechcc.csc.database;

import static edu.faytechcc.csc.database.CreateLogInDB.buildLogInTable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * Please describe what this class does here.
 *
 * CSC 251 0001 DATE:
 *
 * @author Stephanie Mckenzie
 */
public class AddUser
{
    private final String DB_URL = "jdbc:derby:LogInDB";

    public void addNameAndPassword(String name, String password)
    {

        try
        {
            // Create a connection to the database.
            Connection conn =
                    DriverManager.getConnection(DB_URL);

            Statement stmt = conn.createStatement();
            stmt.execute("INSERT INTO LogIn VALUES ( "
                    + "'" + name + "', "
                    + "'" + password + "' )");
            
            // Close the connection.
            conn.close();
        } catch (Exception ex)
        {
            System.out.println("ERROR: " + ex.getMessage());
        }

    }

    public boolean checkExistance(String name)
    {
        
        try
        {
            Connection conn = DriverManager.getConnection(DB_URL); //connect
            Statement stmt = conn.createStatement();
            String sqlStatement =
                    "SELECT * FROM LogIn "
                    + "WHERE UserName = '" + name + "'";
            ResultSet result = stmt.executeQuery(sqlStatement);
            if (result.next())
            {
                String nameResult = result.getString("UserName");

                //convert to lowercase so it's not case sensitive.
                nameResult = nameResult.toLowerCase();
                name = name.toLowerCase();

                //compare name and nameResult
                return (name.compareToIgnoreCase(nameResult) == 0);
            }
        } catch (Exception ex)
        {
            System.out.println("ERROR: " + ex.getMessage());
        }
        return false;
    }
}
